package test5.lupeng.jf.com.teamdemo.activitys;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.lang.reflect.Array;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.bean.RequestSend;
import test5.lupeng.jf.com.teamdemo.bean.RequestTalk;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

//评论文章界面
public class TalkTitleActivity extends AppCompatActivity {

    private static final String TAG = "TalkTitleActivity";
    private String content;
    private String articleId;
    private int articleType;
    private EditText ed_write;
    private String userId;
    private RequestSend requestSend;
    private ProgressDialog dialog;
    private LocalBroadcastManager localBroadcastManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talk_title);

        dialog = new ProgressDialog(this);

        localBroadcastManager = LocalBroadcastManager.getInstance(this);


        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        //自己的id
        userId = sp.getString("objectId","0");

        ed_write = (EditText) findViewById(R.id.ed_write);

        articleId = getIntent().getStringExtra("articleId");
        articleType = getIntent().getIntExtra("articleType", 0);


    }

    //发表按钮
    public void send(View view) {
        String message = ed_write.getText().toString();
        if (message.equals("")) {
            Toast.makeText(this, "内容不能为空", Toast.LENGTH_LONG).show();
        }else {
            String[] eit = new String[]{};
            RequestTalk talk = new RequestTalk(userId, articleId, message, articleType, eit);

            //发布评论
            RequestTitle(talk);

        }

    }

    //发布评论
    public void RequestTitle(RequestTalk talk){
        String url = "https://leancloud.cn:443/1.1/functions/commetnJFArticle";
        Request request = HTTPUtils.requestPost(talk, url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: "+result);

                if (response.code() == 200) {

                    System.out.println("评论成功");

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            //这里差一个弹出框

                            //发送一个广播
                            Intent intent1 = new Intent("SEND_MESSAGE");
                            localBroadcastManager.sendBroadcast(intent1);
                            System.out.println("发送广播啦");


                            Intent intent = new Intent();
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    });


                }
            }
        });


    }



    //点击取消按钮
    public void newReturn1(View view) {
        finish();
    }
}
